package com.gavinandre.mvpsocketclient.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gavinandre.mvpsocketclient.R;
import com.gavinandre.mvpsocketclient.utils.ServerResponseThread;
import com.gavinandre.mvpsocketclient.utils.SocketServerResponseInterface;
import com.gavinandre.mvpsocketclient.utils.SocketUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ServerActivity extends AppCompatActivity implements ServerResponseThread.OnUpdataListener{
    private static boolean isStart = true;
    private static ServerResponseThread serverResponseThread;
    @BindView(R.id.edittext_server)
    EditText mEdittextServer;
    @BindView(R.id.button_send_server)
    Button mButtonSendServer;
    @BindView(R.id.textview_server_response)
    TextView mTextviewServerResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        ButterKnife.bind(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                init();
            }
        }).start();


    }

    private void init() {
        ServerSocket serverSocket = null;
        ExecutorService executorService = Executors.newCachedThreadPool();
        System.out.println("服务端 " + SocketUtil.getIP() + " 运行中...\n");
        try {
            serverSocket = new ServerSocket(SocketUtil.PORT);
            while (isStart) {
                Socket socket = serverSocket.accept();
                //设定输入流读取阻塞超时时间(10秒收不到客户端消息判定断线)
                socket.setSoTimeout(10000);
                serverResponseThread = new ServerResponseThread(socket,
                        new SocketServerResponseInterface() {

                            @Override
                            public void clientOffline() {// 对方不在线
                                System.out.println("offline");
                            }

                            @Override
                            public void clientOnline(String clientIp) {
                                System.out.println(clientIp + " is online");
                                System.out.println("-----------------------------------------");
                            }
                        });

                serverResponseThread.setOnUpdataListener(this);

                if (socket.isConnected()) {
                    executorService.execute(serverResponseThread);
                }
            }

            serverSocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                try {
                    isStart = false;
                    serverSocket.close();
                    serverResponseThread.stop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @OnClick(R.id.button_send_server)
    public void onViewClicked() {
      String msg = mEdittextServer.getText().toString().trim();
        serverResponseThread.addMessage(msg);

    }

    @Override
    public void upDataUI(String message) {
        //将接收到的消息显示
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               mTextviewServerResponse.setText(message);

           }
       });


    }
}
